<?php
namespace Anls\Com\Strategy;
use Anls\Common;
/**
 * 数据分析
 * @author w.
 *
 */
class DataAnls extends AbstractStrategySupport{

    // 最高盈利少于等于N的连续次数
    private $highWinLt = null;
    
    // 平仓盈利少于等于N的连续次数
    private $closeWinLt = null;
    
    public function exec($code){
        $buyTradeData = $this->getBuyTradeLog($code);
        $len = count($buyTradeData);
        for($i=0;$i<$len;$i++){
            $buyTradeData[$i] = json_decode($buyTradeData[$i],true);
        }
        // 重设分析缓存
        Common\Redis::deleteKey($this->redis, Common\Redis::$TRADE_ANLS_KEY.$code);
        // 输赢次数
        $this->buyTradeAnls['winLose'] = $this->winLose($buyTradeData);
        
        // 连续亏损次数分析
        $this->buyTradeAnls['loseLimit'] = $this->loseLimit($buyTradeData);
        
        // 最高盈利少于等于N的连续次数
        $ltVal = $this->getHighWinLt() == null?'3%':$this->getHighWinLt();
        $this->buyTradeAnls['highWinLt'] = $this->highWinLt($buyTradeData,$ltVal);
        
        // 平仓盈利少于等于N的连续次数
        $ltVal = $this->getCloseWinLt() == null?'0%':$this->getCloseWinLt();
        $this->buyTradeAnls['closeWinLt'] = $this->closeWinLt($buyTradeData,$ltVal);
        
        $this->redis->set($this->getCodeAnlsCacheKey($code),json_encode($this->buyTradeAnls));
    }
    
    public function winLose($tradeData){
        $rst = array();
        $win = 0;
        $lose = 0;
        $profitPoint = 0;
        $profitPercent = 0;
        $len = count($tradeData);
        for($i=0;$i<$len;$i++){
            $trade = $tradeData[$i];
            if($trade['buyWinPoint']<=0){
                $lose++;
            } else {
                $win++;
            }
            $profitPoint = $profitPoint + $trade['buyWinPoint'];
            $profitPercent = $profitPercent + $trade['bwp'];
        }
        $rst['win'] = $win;
        $rst['lose'] = $lose;
        $rst['profitPoint'] = $profitPoint;
        $rst['profitPercent'] = $profitPercent;
        return $rst;
    }
    
    
    /**
     * 极限亏损分析
     * @param array $tradeLog
     * @return array
     */
    public function loseLimit($buyTradeData){
        $everyLose = 0;
        $everyLoseSave = array();
        $loseAnls = array();
        $len = count($buyTradeData);
        for($i=0;$i<$len;$i++){
            $trade = $buyTradeData[$i];
            if($trade['buyWinPoint']<=0){
                // 输
                $everyLose++;
            } else {
                if($everyLose==0)continue;
                if($everyLoseSave[$everyLose]!==null){
                    $everyLoseSave[$everyLose] = $everyLoseSave[$everyLose] + 1;
                } else {
                    $everyLoseSave[$everyLose] = 1;
                }
                $everyLose = 0;
            }
        }
        $everyLoseSave['cur'] = $everyLose;
        return $everyLoseSave;
    }
    
    public function closeWinLt($tradeData,$ltVal){
        $everyLose = 0;
        $everyLoseSave = array();
        $loseAnls = array();
        $len = count($tradeData);
        $isPercent = strpos($ltVal, '%') == false?false:true;
        $ltVal = str_replace('%','',$ltVal);
        for($i=0;$i<$len;$i++){
            $trade = $tradeData[$i];
            $bmhp = 0;
            if($isPercent){
                $bmhp = $trade['bmcp'];
            } else {
                $bmhp = $trade['buyMaxCloseProfit'];
            }
    
            if(floatval($bmhp)<=floatval($ltVal)){
                // 输
                $everyLose++;
            } else {
                if($everyLose==0)continue;
                if($everyLoseSave[$everyLose]!==null){
                    $everyLoseSave[$everyLose] = $everyLoseSave[$everyLose] + 1;
                } else {
                    $everyLoseSave[$everyLose] = 1;
                }
                $everyLose = 0;
            }
        }
        $everyLoseSave['cur'] = $everyLose;
        return $everyLoseSave;
    }
    
    public function highWinLt($tradeData,$ltVal){
        $everyLose = 0;
        $everyLoseSave = array();
        $loseAnls = array();
        $len = count($tradeData);
        $isPercent = strpos($ltVal, '%') == false?false:true;
        $ltVal = str_replace('%','',$ltVal);
        for($i=0;$i<$len;$i++){
            $trade = $tradeData[$i];
            $bmhp = 0;
            if($isPercent){
                $bmhp = $trade['bmhp'];
            } else {
                $bmhp = $trade['buyMaxHighProfit'];
            }
            
            if(floatval($bmhp)<floatval($ltVal)){
                // 输
                $everyLose++;
            } else {
                if($everyLose==0)continue;
                if($everyLoseSave[$everyLose]!==null){
                    $everyLoseSave[$everyLose] = $everyLoseSave[$everyLose] + 1;
                } else {
                    $everyLoseSave[$everyLose] = 1;
                }
                $everyLose = 0;
            }
        }
        
        $everyLoseSave['cur'] = $everyLose;
        $everyLoseSave['cal<'] = $ltVal;
        return $everyLoseSave;
    }
    
    

    public function setHighWinLt($highWinLt){
        $this->highWinLt = $highWinLt;
    }
    
    public function getHighWinLt(){
        return $this->highWinLt;
    }
    
    public function setCloseWinLt($closeWinLt){
        $this->closeWinLt = $closeWinLt;
    }
    
    public function getCloseWinLt(){
        return $this->closeWinLt;
    }
	
}

?>
